package com.example.user.view;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.gamedetail.GameDetailActivity;
import com.example.common.network.IStartGameDetailActivity;
import com.example.user.MyGameRAdapter;
import com.example.user.R;
import com.example.user.databinding.UserMyGameFragmentBinding;
import com.example.user.viewmodel.MyGamePageViewModel;
import com.example.user.viewmodel.UserMyProfileDetailViewModel;

/*
* 用户页面的主页
* */
public class MyGamePageFragment extends Fragment {

    private MyGamePageViewModel viewModel;
    private UserMyGameFragmentBinding binding;
    private MyGameRAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //数据绑定
        binding= DataBindingUtil.inflate(inflater,R.layout.user_my_game_fragment,container,false);
        binding.setLifecycleOwner(this);

        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity()).get(MyGamePageViewModel.class);
        //获取父容器的viewmodel
        UserMyProfileDetailViewModel parentViewModel=new ViewModelProvider(requireActivity()).get(UserMyProfileDetailViewModel.class);
        // TODO: Use the ViewModel

        //recyclerView 绑定
        adapter=new MyGameRAdapter(viewModel);
        binding.userMyGameRecyclerView.setAdapter(adapter);
        binding.userMyGameRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(),4));

        //绑定路由的点击事件
        adapter.iStartGameDetailActivity = new IStartGameDetailActivity() {
            @Override
            public void openById(Integer id) {
                Intent intent = new Intent(requireActivity(), GameDetailActivity.class);
                intent.putExtra("ID", id);
                startActivity(intent);
            }
        };

        parentViewModel.getIsLoad().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    //加载游戏列表
                    viewModel.loadGamesList();
                }
            }
        });

        //监听刷新adapter
        viewModel.getIsLoadDone().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    adapter.notifyDataSetChanged();
                    viewModel.getIsLoadDone().setValue(false);
                }
            }
        });
    }

}